package com.xuxk.project.library.controller.api;

import com.github.pagehelper.Page;
import com.xuxk.project.library.annotation.LibraryLog;
import com.xuxk.project.library.bean.XuxkLibrary;
import com.xuxk.project.library.service.XuxkLibraryService;
import com.xuxk.project.library.util.PageInfoVo;
import com.xuxk.project.library.util.PageUtil;
import com.xuxk.project.library.util.RequestUtils;
import com.xuxk.project.library.util.ResultBodyVo;
import com.xuxk.project.library.vo.XuxkLibraryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *   @author  橙云科技有限公司
 */

@RequestMapping("/api/xuxkLibrary")
@RestController
public class XuxkLibraryController   {

    @Autowired
	XuxkLibraryService xuxkLibraryService;


    /**
    * 分页查询
    * @param pageNum  页码
    * @param pageSize 页行数
    * @param request
    * @return
    */
    @GetMapping(value = "/v1.0/xuxkLibrarys", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "分页查询图书馆信息列表")
	public ResultBodyVo xuxkLibrarys(Integer pageNum, Integer pageSize, HttpServletRequest request){

        Map<String,Object> paramMap = RequestUtils.getQueryParams(request);
        Page<XuxkLibraryVo> pageResult = xuxkLibraryService.listXuxkLibrarys(paramMap, PageUtil.getPageParam(pageNum,pageSize));

		return new ResultBodyVo(new PageInfoVo(pageResult)) ;
	}




    @GetMapping(value="/v1.0/xuxkLibrarys/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "获取图书馆信息")
    public ResultBodyVo getXuxkLibrary(@PathVariable Integer id) {
        XuxkLibrary xuxkLibrary = xuxkLibraryService.getXuxkLibrary(id,null);

        return new ResultBodyVo(xuxkLibrary);
    }


    @PostMapping(value="/v1.0/xuxkLibrarys", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "新增图书馆信息")
    public ResultBodyVo saveXuxkLibrary(@RequestBody XuxkLibrary xuxkLibrary) {
        return  new ResultBodyVo(xuxkLibraryService.insertXuxkLibrary(xuxkLibrary));

    }


    @PatchMapping(value="/v1.0/xuxkLibrarys/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "修改部分图书馆信息")
    public ResultBodyVo patchUpdateXuxkLibrary(@PathVariable Integer id,@RequestBody XuxkLibrary xuxkLibrary) {
        xuxkLibrary.setId(id);
        return  new ResultBodyVo(xuxkLibraryService.patchUpdateXuxkLibrary(xuxkLibrary));


    }

    @PutMapping(value="/v1.0/xuxkLibrarys/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultBodyVo updateXuxkLibrary(@PathVariable Integer id,@RequestBody XuxkLibrary xuxkLibrary) {

        xuxkLibrary.setId(id);
        return new ResultBodyVo(xuxkLibraryService.updateXuxkLibrary(xuxkLibrary));
    }


    @DeleteMapping(value="/v1.0/xuxkLibrarys/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @LibraryLog(eventName = "删除图书馆信息")
    public ResultBodyVo deleteXuxkLibrary(@PathVariable Integer id) {

         xuxkLibraryService.deleteXuxkLibrary(id );

        return new ResultBodyVo("删除成功") ;

    }

}
